import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const PreviewImage 			= $Lazy.state(()=>import('#private/pages/preview-image'))
const PreviewReportModal 	= $Lazy.state(()=>import('./cpt/preview-report-modal'))
const ClientChangeModal 	= $Lazy.state(()=>import('./cpt/client-change-modal'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '标本条码',		name: 'spec_code' },
			{ label: '状态',			name: 'status',					 	 type: 'select', },
			{ label: '类型',			name: 'type',						 type: 'select', },
			{ label: '创建时间',  	names: ['start_date','end_date'],	 type:'date-range',format:1 },
		],
		// 提交表单列表
		formData : [
			{ label: '标本条码',		name: 'spec_code', 		half:true, 	required:true, onValue:({ value })=>{
				if(value.length === 12 || value.length === 14){
					$http.pull(null,'specimen/views', { param:{ spec_code:value, infochange:1 }, loading:true }).then(data=>{
						if($fn.hasArray(data)){
							const { patient_name, hosp_id } = data[0]
							const kinds = data.map(({parent_kind_name}) => parent_kind_name)
							const kind_names = kinds.join(",")
							this.formRef.setValue({ patient_name, hosp_id, kind_names })
						}else{
							this.formRef.setValue({ patient_name:'', hosp_id:'', kind_names:'' })
						}
					})
				}
			} },
			{ label: '类型',			name: 'type', 			half:true, 	type: 'select', required:true },
			{ label: '姓名',			name: 'patient_name', 	half:true, 	required:true },
			{ label: '医院',			name: 'hosp_id', 		half:true,  type: 'select' },
			{ label: '反馈人',		name: 'feedback',		half:true, },
			{ label: '检测项目',		name: 'kind_names', 	full:true,  type: 'textarea', disabled:true },
			{ label: '图片',        name: 'img', 			full:true, 	type:'upload', multiple:true, code:130 },
			{ label: '内容',        name: 'content', 		full:true, 	type:'textarea' },
			{ label: '备注',        name: 'remark', 			full:true, 	type:'textarea' },
		]
	}
	
	query = { status:'', type:'' }
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
		  this.fetch()
		}
		$fn.fresh.on(this.fetch)
	}
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 状态
			const statusData = $fn.setRow(forms, 'status',  await f.treatment_state)
			// 类型
			const typeData = $fn.setRows([ forms, formData ], 'type',  await f.modification_type)
			
			this.setState({ forms, statusData, typeData })
		})
	}
	
	// 获取弹窗下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'hosp_id', ()=>{
			$cache.then( async f => {
				//医院
				$fn.setRow(formData, 'hosp_id', await f.getCache({api:'hospital'}))
				// 
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'sp-info-modify/index')
	
	// 表单
	cols = [
		{ title: '条码号',			dataIndex: 'spec_code', 	...$fn.table.spec_code },
		{ title: '类型', 			dataIndex: 'type', 			width:100, render: text => $fn.getState(this.state.typeData, text) },
		{ title: '姓名', 			dataIndex: 'patient_name', 	...$fn.table.name },
		{ title: '医院', 			dataIndex: 'hosp_name', 	width:200 },
		{ title: '内容', 			dataIndex: 'content', 		width:150 },
		{ title: '创建时间', 		dataIndex: 'created_at', 	...$fn.table.time },
		{ title: '状态', 			dataIndex: 'status', 		width:80, render: text => $fn.getState(this.state.statusData, text) },
		TableControls.controls.call(this, { width: 445, align:'left' })
	]
	// 表格操作按钮
	table_controls = ( text, { uuid, status, spec_code, type, imgs }) => {
		status = +status
		type = +type
		return [		
			TableControls.click.call(this, '预览报告单', () => this.reportRef.open(spec_code,uuid) ),
			// 待处理
			status === 0 && TableControls.edit.call(this, 'sp-info-modify/info', { uuid }, { select:this.getModalSelect }),	// 编辑
			status === 0 && Confirm.call(this, '审核修改申请', 'sp-info-modify/audit', { param: uuid }),	// 审核
			// 已处理
			status === 1 && Confirm.call(this, '完成', 'sp-info-modify/complete', { param: uuid }),	// 完成
			// 修改资料
			(type === 1 && status === 0) && TableControls.pushTo.call(this, '修改资料', `patient-detail?spec_code=${spec_code}&page_source=kf`),
			// 修改医院信息
			type === 2 && TableControls.click.call(this,'客户变更', ()=> this.clientRef.open(spec_code) ),
			$fn.hasArray(imgs) && TableControls.click.call(this,'查看图片', ()=> this.imgRef.open(imgs ), { ghost:true })
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: this.getModalSelect }),
			Controls.exports.call(this, 'sp-info-modify/export', { param:{ status:'', type:'' } }),
		]
	}
	render(){
		return (
			<>
				<Page title='信息修改' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api:'sp-info-modify/add', editApi:'sp-info-modify/edit' }) }
				{/* 预览报告单 */}
				<PreviewReportModal onRef={ref=>this.reportRef = ref} {...this.props}/>
				{/* 查看图片 */}
				<PreviewImage onRef={ref=>this.imgRef = ref} />
				{/* 客户变更 */}
				<ClientChangeModal onRef={ref=>this.clientRef = ref} onRefresh={ this.fetch } />
			</>
		)
	}
}

export default Index